package com.edu.juc.collections.list;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 集合线程安全问题
 * List集合线程不安全
 */
public class ArrayListTest {
    public static void main(String[] args) {
//        MList mList = new MList();
//        ArrayList<Integer> list = mList.getList();

//        ArrayList<Integer> list1 = new ArrayList<>();
        // 1. Vector 解决集合线程安全问题
//        Vector<Integer> list = new Vector<>();
        // Collections.synchronizedList()
        // 2. Collections 解决集合线程安全问题
//        List<Integer> list = Collections.synchronizedList(new ArrayList<>());
        // 3. CopyOnWriteArrayList 解决集合线程安全问题
        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                list.add(i);
            }
            System.out.println("list.size() = " + list.size());
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                list.add(i);
            }
            System.out.println("list.size() = " + list.size());
        }).start();

    }
}
